package com.object.module.didi.service.impl;

import com.object.configuration.utils.PasswordEncryp;
import com.object.configuration.utils.Q;
import lombok.val;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;

import com.object.dao.didi.DdUserDao;
import com.object.module.didi.entity.DdUserEntity;
import com.object.module.didi.service.DdUserService;


@Service("ddUserService")
public class DdUserServiceImpl extends ServiceImpl<DdUserDao, DdUserEntity> implements DdUserService {
    @Autowired
    private DdUserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DdUserEntity> page = this.page(
                new Query<DdUserEntity>().getPage(params),
                new QueryWrapper<DdUserEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 登录
     *
     * @param userEntity
     * @return
     */
    @Override
    public DdUserEntity login(DdUserEntity userEntity) {
        QueryWrapper<DdUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ur_name", userEntity.getUrName());
        DdUserEntity userEntity1 = getOne(wrapper);
        if (PasswordEncryp.IfPassowrd(userEntity.getUrPassword(), userEntity1.getUrPassword())) {
            return userEntity1;
        }
        return null;
    }

    /**
     * 注册
     *
     * @param ddUser
     * @param
     * @return
     */
    @Override
    public Q saveUser(DdUserEntity ddUser) {
        if (PasswordEncryp.IfPassowrd(ddUser.getUrCaptcha(),ddUser.getEncrypts())) {
            DdUserEntity userEntity = getOne(new QueryWrapper<DdUserEntity>().eq("ur_name", ddUser.getUrName()));
            if (userEntity != null) {
                return Q.error().message("用户名存在请重新注册!");
            }
            ddUser.setUrPassword(PasswordEncryp.getPaawordEncryp(ddUser.getUrPassword()));
            ddUser.setReId(1);
            ddUser.setUrNickName(ddUser.getUrName());
            String qq = ddUser.getUrQq();
            ddUser.setUrAvatar("https://q.qlogo.cn/headimg_dl?dst_uin=" + qq + "&spec=100");
            save(ddUser);
        } else {
            return Q.error().message("注册失败验证码错误!");
        }
        return Q.ok().message("注册成功,可以去登录!");
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @Override
    public boolean updaatePassword(DdUserEntity user) {
        val urId = user.getUrId();
        var userEntity = getById(urId);
        if (PasswordEncryp.IfPassowrd( user.getUrPassword(),userEntity.getUrPassword())) {
            String password = PasswordEncryp.getPaawordEncryp(user.getNewPassword());
            boolean flag = userDao.updatePassword(password, urId);
            return flag;
        }
        return false;
    }

}